//导入红外接收相关库
#include <IRremote.h>
#include <IRremoteInt.h>
//红外信号接受引脚
int IFR_PIN = 3;
//初始化红外接收相关设置
IRrecv irrecv(IFR_PIN);
//定义接收到的红外信息
decode_results RECV;

//定义LED总亮度引脚
int POWER = 5;
//定义LED呼吸频率引脚
int POWERPWM = 6;
//定义RGB三色LED引脚
int R = 9;
int G = 10;
int B = 11;
//定义遥控指示灯
int ledPin = 13;
//定义LED总亮度
int bright = 255;
//定义LED呼吸频率
int PWM = 255;
//定于RGB三色引脚颜色深度（占空比）
int RPWM = 255;
int GPWM = 255;
int BPWM = 255;
//定义每变化一位深度所需时间
int t = 1000;
//定义灯光是否长亮
boolean lasting = 0;
//定义ASCII码与0~9数字的转换数组
static int num[10] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57};
//定义遥控器二维数组
static long telecontroller[7][3] = {
  {16753245, 16736925, 16769565},
  {16720605, 16712445, 16761405},
  {16769055, 16754775, 16748655},
  {16738455, 16750695, 16756815},
  {16724175, 16718055, 16743045},
  {16716015, 16726215, 16734885},
  {16728765, 16730805, 16732845},
};
//初始化Ardunio
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn(); // 开始接收红外信息
}

////读取串口信息（调试使用，可忽略）
//void SerialRead() {
//  //获取串口数据数量位
//  int serialNum = Serial.available();
//  if (serialNum > 0) {
//    //初始化时间
//    t = 0;
//    for (int i = 0; i < serialNum; i++) {
//      //读取串口数据一位
//      int ascii = Serial.read();
//      for (int numarr = 0; numarr < 10; numarr++) {
//        //判断串口数据ascii码是哪个数字(0~9)
//        if ( ascii == num[numarr]) {
//          //对读取出的时间数据赋值
//          t = t + numarr * pow(10, serialNum  - 1 - i);
//        }
//      }
//      delay(5);
//    }
//    Serial.println(t);
//  }
//}

//接收红外信号
void IFR() {
  if (irrecv.decode(&RECV)) {
    Serial.println(RECV.value);
    //遥控提示灯
    if (RECV.value != 4294967295) {
      digitalWrite(ledPin, HIGH);
      delay(50);
      digitalWrite(ledPin, LOW);
    }
    //降低灯光亮度
    if (RECV.value == telecontroller[0][0]) {
      if (bright >= 10) {
        bright = bright - 10;
      }
      Serial.println(bright);
    }
    //灯光开关
    if (RECV.value == telecontroller[0][1]) {
      if (bright != 0) {
        bright = 0;
      } else {
        bright = 255;
      }
      Serial.println(bright);
    }
    //增加灯光亮度
    if (RECV.value == telecontroller[0][2]) {
      if (bright <= 245) {
        bright = bright + 10;
      }
      Serial.println(bright);
    }
    //减少灯光呼吸时间
    if (RECV.value == telecontroller[1][0]) {
      if (t > 500) {
        t = t - 500;
      }
      Serial.println(t);
    }
    //减少灯光呼吸时间
    if (RECV.value == telecontroller[1][1]) {
      if (t < 12000) {
        t = t + 500;
      }
      Serial.println(t);
    }
    //灯光常亮
    if (RECV.value == telecontroller[1][2]) {
      if (lasting == 0) {
        lasting = 1;
      } else {
        lasting = 0;
      }
      Serial.println(lasting);
    }
  }
  irrecv.resume(); // 接收下一个值
}
//主循环函数
void loop() {
//  SerialRead();
  IFR();
  //LED亮度
  analogWrite(POWER, bright);
  //LED三色灯
  analogWrite(R, RPWM);
  analogWrite(G, GPWM);
  analogWrite(B, BPWM);
  if (lasting == 0) {
    //呼吸灯
    for (int i = 0; i <= PWM; i++) {
      analogWrite(POWERPWM, i);
      delayMicroseconds(t);
    }
    for (int i = 0; i <= PWM; i++) {
      analogWrite(POWERPWM, PWM - i);
      delayMicroseconds(t);
    }
  } else {
    analogWrite(POWERPWM, PWM);
    delay(500);
  }


}
